/**  File: mapbuilder.cpp
 *   Author: Jaideep Balekar
 *           Department of Computer Science
 *           Texas A&M University
 *           315 - Programming Studio
 * Due Date: December 6th, 2009 @ 11:59
 * Contents: Build the game map by taking the input from inmap.txt file. 
			 Reads the text file and stores the coordinated of the char'+' in a list
 */


#include "mapbuilder.h"
#include<QDebug>

MapBuilder::MapBuilder() //constructor
{
    xcoord = 0;
    ycoord = 0;
}

MapBuilder::~MapBuilder() //destructor
{
}

void MapBuilder::fileRead() //reads the text file, looks for '+' char, stores its co ordinates in a list.
{

     QFile file("inputmap.txt"); //create file while points to the  map text file
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;

     CoordChar cc;

     QTextStream in(&file); //QTextStream in = new QTextStream(&file);
     while (!in.atEnd()) //bool atEnd() :Returns true if there is no more data to be read from the QTextStream
	 { // keep reading until the end of file is reached
         QString line = in.readLine(); //readLine(): Reads one line of text from the stream, and returns it as a QString
         for (int i = 0; i < line.size(); ++i)
         {
            QString key = QString::number(xcoord).append(QChar('_')).append(QString::number(ycoord));
            QString *playerKey;
            switch(line.at(i).toAscii()) //at(): Returns the character at the given index position in the string
            {							//toAscii(): returns an 8-bit ASCII representation of the string

                case '+':
                        cc.xcoord = xcoord;
                        cc.ycoord = ycoord;
                        cc.symbol = '+';
                        map.insert(key, cc);

                        //Map for player movement
                        cc.xcoord = xcoord*10;
                        cc.ycoord = ycoord*10;
                        playerKey = new QString(QChar(xcoord*10));
                        playerKey->append(QChar('_')).append(QChar(ycoord*10));
                        sceneItemsMap.insert(*playerKey,cc);
                        break;

                case '.':
                        cc.xcoord = xcoord;
                        cc.ycoord = ycoord;
                        cc.symbol = '.';
                        dotmap.insert(key, cc);

                        //Map for player movement
                        cc.xcoord = xcoord*10;
                        cc.ycoord = ycoord*10;
                        playerKey = new QString(QChar(xcoord*10));
                        playerKey->append(QChar('_')).append(QChar(ycoord*10));
                        sceneItemsMap.insert(*playerKey,cc);
                        break;

                case 'O':
                        cc.xcoord = xcoord;
                        cc.ycoord = ycoord;
                        cc.symbol = 'O';
                        powerdotmap.insert(key, cc);

                        //Map for player movement
                        cc.xcoord = xcoord*10;
                        cc.ycoord = ycoord*10;
                        playerKey = new QString(QChar(xcoord*10));
                        playerKey->append(QChar('_')).append(QChar(ycoord*10));
                        sceneItemsMap.insert(*playerKey,cc);
                        break;
            }//switch ends
            xcoord++;
         }//for loop ends
         ycoord++;
         xcoord = 0;
     }//while ends. finished reading from stream
}




